package com.sali.数据结构;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 栈的压入、弹出序列
 */
public class JZ_31 {

    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Deque<Integer> myStack = new ArrayDeque<>();

        int j = 0;
        for ( int i = 0; i < popV.length; i++ ) {
            while ( j < pushV.length && ( myStack.isEmpty() || myStack.peek() != popV[i] ) ) {
                myStack.push(pushV[j]);
                j++;
            }
            if ( myStack.peek() != popV[i] ) {
                return false;
            } else {
                myStack.pop();
            }
        }

        return myStack.isEmpty();
    }

}
